package com.rent.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.rent.bean.Brand;
import com.rent.bean.Type;
import com.rent.dao.TypeDao;
import com.rent.utils.BaseDao;
import com.rent.utils.DataAccessUtils;

public class TypeDaoImpl extends BaseDao implements TypeDao {
	DataAccessUtils d = new DataAccessUtils();
	@Override
	public List<Type> seleByBrand(String brand,int grade) {
		String sql = "select DISTINCT type from car_new where cbrand = ? and cstatus =1";
		if(grade==1) {
			sql+=" and cgrade =1";
		}
		ResultSet rs = executeQuery(sql, brand);
		List<Type> types = d.parseType(rs);
		return types;
	}
	@Override
	public int add(Type t) {
		String sql = "insert into car_type values(null,?,?,?);";
		return executeUpdate(sql, t.getBid(),t.getType(),t.getPrice());
	}
	@Override
	public int update(Type t) {
		String sql = "update car_type set bid=?,type=?,price=? where tid=?;";
		return executeUpdate(sql, t.getBid(),t.getType(),t.getPrice(),t.getTid());
	}
	@Override
	public List<Type> seleByType(Type t) {
		String sql = "select * from car_type_brand where 1=1";
		if(t!=null) {
			if(t.getBid()!=0) {
				sql+=" and bid="+t.getBid();
			}
			if(t.getType()!=null) {
				sql+=" and type = '"+t.getType()+"'";
			}
			if(t.getTid()!=0) {
				sql+=" and tid = "+t.getTid();
			}
			if(t.getPrice()!=0) {
				sql+=" and price <"+t.getPrice();
			}
		}
		sql +=" order by tid";
		ResultSet rs = executeQuery(sql);
		List<Type> parseType = d.parseType(rs);
		return parseType;
	}
	@Override
	public Type seleById(int tid) {
		String sql = "select * from car_type where tid=?;";
		ResultSet rs = executeQuery(sql, tid);
		Type b = null;
		try {
			if(rs.next()) {
				b = new Type(rs.getInt("tid"),rs.getInt("bid"),rs.getString("type"),rs.getDouble("price"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(rs);
		}
		return b;
	}

}
